package com.suncnpap.library.controller;

import com.suncnpap.library.model.BookLoss;
import com.suncnpap.library.model.BorrowBookInfo;
import com.suncnpap.library.model.BorrowBookRecord;
import com.suncnpap.library.model.CollectionChange;
import com.suncnpap.library.model.ReturnBook;
import com.suncnpap.library.service.BookLossService;
import com.suncnpap.library.service.BorrowBookRecordService;
import com.suncnpap.library.service.BorrowBookService;
import com.suncnpap.library.service.ReturnBookService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.app.util.beanutil.BeanUtil;
import com.app.util.common.HandlerResult;
import com.app.util.common.ObjUtil;
import com.app.util.common.ResponseUtil;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.app.util.database.mybatis.QueryType;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.datamanage.common.ExportExcelUtils;

/** 
 * 说明：图书丢失Controller
 */
@Controller
@RequestMapping(value="/library/bookLoss")
public class BookLossController extends BaseController {
	
	//private static final Logger log = Logger.getLogger(BookLossController.class);
	@Autowired
	BookLossService bookLossService;
	
	@Autowired
	BorrowBookRecordService borrowBookRecordService;
	
	@Autowired
	BorrowBookService borrowBookService;
	
	@Autowired
	ReturnBookService returnBookService;
	/**
	 * 去往列表页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
//			//所属书屋
//			String strISBN = this.getParameter("strISBN","");	//ISBN
//			if(!"".equals(strISBN)){
//				page.addQueryAnd("strISBN", QueryType.markLIKE, strISBN);
//			}
//			
			page.removeQueryAnd("strSchoolId");
			page.addQueryAnd("br.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
			
			page.put("user", "true");
			page.put("isUnit", "true");
			page.addQueryAnd("r.strSchoolId", result.get("strSchoolId"));
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String strType = this.getParameter("strType", "");
			if(!strType.equals("")){
				page.addQueryAnd("br.strType", strType);
			}
			
			page.addQueryAnd("r.strSchoolId", result.get("strSchoolId"));
			page.addQueryAnd("l.strSchoolId", result.get("strSchoolId"));
			
			page = bookLossService.listInfo(page);
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<BorrowBookInfo> list = page.getDataList();
				String header = "图书条码,正题名,所属书屋,读者卡号,姓名,部门,赔偿金额,操作时间,丢失、损毁,操作员";

				String tableStr = "strBookBarcode,strPositiveTitle,strCollectionLocation,strReaderCardNumber,strReaderName,strUnitName,strAmountPaid,dtCreatDate,strType,strCreatName";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("图书丢失损毁", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),list, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(@RequestBody BookLoss obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strSchoolId = String.valueOf(result.get("strSchoolId"));
				
				BookLoss bookLoss = bookLossService.findByBookBarcode(obj.getStrBookBarcode(), strSchoolId);
				if(bookLoss==null){
					super.init(obj);
					obj.setStrAmountReceipt(obj.getStrAmountPaid());
					bookLossService.add(obj);
					
					BorrowBookRecord borrowBookRecord = borrowBookRecordService.findByBookBarcode(obj.getStrBookBarcode(), strSchoolId);
					
					if(borrowBookRecord!=null){
						
						borrowBookService.deleteByBookBarcode(obj.getStrBookBarcode(), strSchoolId);
						
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
						borrowBookRecord.setDtReturnDate(sdf.parse(sdf.format(new Date())));
						borrowBookRecord.setIsReturn(1);
						borrowBookRecordService.update(borrowBookRecord);
						
						ReturnBook returnTemp = new ReturnBook();
						BeanUtil.copyBean(borrowBookRecord, returnTemp);
						init(returnTemp);
						returnBookService.add(returnTemp);
					}
				}else{
					result.setCode(1);
					result.setMessage("该图书已经丢失/损毁");
				}
				
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	
	/**
	 * 删除处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody BookLoss obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strSchoolId = String.valueOf(result.get("strSchoolId"));
				
				String strBookBarcode = obj.getStrBookBarcode();
				if(strBookBarcode != null && !strBookBarcode.equals("")){
					bookLossService.deleteByBookBarcode(strBookBarcode, strSchoolId);
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
}
